package com.czy.owner.ui.work;

import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.czy.owner.R;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.CarInfo;
import com.czy.owner.global.Constants;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.utils.PhoneUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import butterknife.BindView;

/**
 * Created by King on 2017-05-18
 */

public class SetMileageActivity extends BaseActivity {
    @BindView(R.id.et_set_mileage)
    EditText etMileage;
    @BindView(R.id.btn_set_mileage_submit)
    Button btnSubmit;

    private CarInfo mCarInfo;


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_set_mileage;
    }

    @Override
    protected void initInjector() {
        if (getIntent().getExtras() == null || (mCarInfo = (CarInfo) getIntent().getSerializableExtra(Constants.TRANSMISSION_CONTENT_KEY)) == null) {
            PhoneUtils.ShowToastMessage(this, "参数异常!");
            finish();
            return;
        }
    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText("行驶里程");
    }

    @Override
    protected void updateViews() {
        etMileage.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() == 0) {
                    btnSubmit.setEnabled(false);
                } else {
                    btnSubmit.setEnabled(true);
                }
            }
        });

        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (etMileage.getText().toString().trim().length() > 0) {
                    RequestParams params = new RequestParams(Constants.APP_API_BASE + Constants.OWN_CAR_UPDATE);
                    btnSubmit.setEnabled(false);
                    mLoadView.ShowLoadView("正在提交数据");
                    params.addBodyParameter("session", UserHelper.getInstance().getSession(SetMileageActivity.this));
                    params.addBodyParameter("currentMileage", etMileage.getText().toString().trim());
                    params.addBodyParameter("carBrandId", "" + mCarInfo.getCarBrandId());
                    params.addBodyParameter("ownerCarId", "" + mCarInfo.getOwnerCarId());
                    params.addBodyParameter("licenseNumber", mCarInfo.getLicenseNumber());

                    params.addBodyParameter("licenseAddTime", "");
                    params.addBodyParameter("buyTime", "");
                    params.addBodyParameter("chassisNumber", "");
                    params.addBodyParameter("engineType", "");

                    Log.i("King",UserHelper.getInstance().getSession(SetMileageActivity.this) + "|" + etMileage.getText().toString().trim() + "|" + mCarInfo.getCarBrandId() + "|" + mCarInfo.getOwnerCarId() + "|" + mCarInfo.getLicenseNumber());
                    x.http().post(params, new Callback.CommonCallback<String>() {
                        @Override
                        public void onSuccess(String result) {
                            String data = PhoneUtils.checkResponseFlag(SetMileageActivity.this, result);
                            if (data != null) {
                                PhoneUtils.ShowToastMessage(SetMileageActivity.this, "行驶量程设置成功!");
                                // TODO 可以考虑修改缓存数据
                                UserHelper.getInstance().getDefaultCar().setCurrentMileage(Long.parseLong(etMileage.getText().toString().trim()));
                                setResult(RESULT_OK);
                                finish();
                            }
                        }

                        @Override
                        public void onError(Throwable ex, boolean isOnCallback) {
                            ex.printStackTrace();
                            PhoneUtils.ShowToastMessage(SetMileageActivity.this, "数据提交异常,请重试");
                        }

                        @Override
                        public void onCancelled(CancelledException cex) {

                        }

                        @Override
                        public void onFinished() {
                            mLoadView.CancleLoadView();
                            btnSubmit.setEnabled(true);
                            mLoadView.CancleLoadView();
                        }
                    });
                }
            }
        });
    }
}
